package com.ebelter.temperaturegun.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.Temperature2BytesMake;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.LoginResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.IConstant;
import com.ebelter.temperaturegun.model.http.TGNetUtils;
import com.ebelter.temperaturegun.utils.StatusBarUtil;

public class LoginActivity extends TBaseActivity {

    public static final String TAG = "LoginActivity";
    EditText et_name_login;
    EditText et_pwd_login;

    TextView btn_login;
    TextView tv_forgot_password;
    TextView btn_register;

    @Override
    protected int setContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        et_name_login=findViewById(R.id.et_name_login);
        et_pwd_login=findViewById(R.id.et_pwd_login);
        btn_login=findViewById(R.id.btn_login);
        tv_forgot_password=findViewById(R.id.tv_forgot_password);
        btn_register=findViewById(R.id.btn_register);

        String name = UserSpUtil.readString(IConstant.IUserSpCon.NICKNAME);
        String psw = UserSpUtil.readString(IConstant.IUserSpCon.PSW);
        if (!TextUtils.isEmpty(name)){
            et_name_login.setText(name);
            if (!TextUtils.isEmpty(psw)){
               login2Service(name,psw);
               // showLoadingDialog(StringUtils.getResStr(R.string.Log_in));
            }
        }
    }


    //跳转到注册界面
    public void gotoRegister(View view) {

        //startActivity(RegisterActivity.class,null);

        Intent intent = new Intent(this,RegisterActivity.class);
        startActivityForResult(intent,RegisterActivity.RequestCode);
    }

    //忘记密码
    public void tv_forgot_password(View view) {
        startActivity(ChangePasswordActivity1.class,null);
    }

    //登录
    public void login(View view) {
        String name = et_name_login.getText().toString().trim();
        String psw = et_pwd_login.getText().toString().trim();
        //  String psw = login_mypsw_cb.getText();

        if (TextUtils.isEmpty(name)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }
        if (TextUtils.isEmpty(psw)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        login2Service(name,MD5Utils.getMd5(psw));


    }

    private void login2Service(String name, final String psw){
        showLoadingDialog(StringUtils.getResStr(R.string.Log_in));


        TGNetUtils.getInstance().loginRequest(this, false, name, psw,
                new HttpResponse<LoginResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestDes, LoginResponse loginResponse,String s) {
                        // SystemClock.sleep(2000);
                        dissLoadingDialog();
                        if (isRequestSuccess){
                            LogUtils.i(TAG,"---loginRequest--loginResponse="+loginResponse);
                            if (loginResponse.resultCode==0){
                                //登录成功
                                saveUserDatas(loginResponse.getResultData());
                                startActivity(MainActivity.class,null);

                                UserSpUtil.writeString(IConstant.IUserSpCon.PSW,psw);
                                finish();
                            }else if (loginResponse.getResultCode() == 109){//109是：不存在的email或者昵称
                                ToastUtil.show(R.string.mail_nickname_no_exist);
                            }else if(loginResponse.getResultCode() == 111){//111密码错误
                                ToastUtil.show(R.string.psw_error);
                            }else if (loginResponse.getResultCode() == 110){//服务器异常
                                ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                            }
                            //הזן את הגובה שלך (%1$.1f ~ %2$.1f)

                        }else {
                            ToastUtil.show(R.string.Network_connection_error);
                        }
                    }
                });

    }


    //保存登录用户数据
    private void saveUserDatas(LoginResponse.ResultDataBean resultData) {
        int srcUnit = UserSpUtil.readInt(IConstant.IUserSpCon.UNIT,0);
        UserSpUtil.removeAll();
        UserSpUtil.writeInt(IConstant.IUserSpCon.UNIT,srcUnit);
        //LogUtils.i(TAG,"---------"+resultData.toString());
        long useridlong = resultData.getUserId();
        UserSpUtil.writeLong(IConstant.IUserSpCon.USERID,useridlong);
        UserSpUtil.writeString(IConstant.IUserSpCon.NICKNAME,resultData.getNickName());
        UserSpUtil.writeString(IConstant.IUserSpCon.EMAIL,resultData.getEmail());
        UserSpUtil.writeInt(IConstant.IUserSpCon.SEX,resultData.getSex());
        //UserSpUtil.writeFloat(IConstant.IUserSpCon.WARRING_UNIT,37);
        //UserSpUtil.writeInt(IConstant.IUserSpCon.UNIT,0);//0C 1F
        UserSpUtil.writeString(IConstant.IUserSpCon.BIRTHDAY,resultData.birthday);//格式 1991-06-05
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RegisterActivity.RequestCode==requestCode&&RegisterActivity.ResultCode==resultCode){
            LogUtils.i(TAG,"---注册界面回来");

            String nickname = data.getStringExtra("n");
            String psw = data.getStringExtra("p");
            LogUtils.i(TAG,"----nickname = "+nickname);

            ViewUtils.setTextViewStr(et_name_login,nickname);
            ViewUtils.setTextViewStr(et_pwd_login,psw);

            login(null);

        }
    }
}
